package com.battery.service.impl;

import com.battery.dao.MerChantDao;
import com.battery.dao.UserDao;
import com.battery.pojo.MerChant;
import com.battery.service.MerChantService;
import com.battery.util.result.Result;
import com.battery.util.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerChantServiceImpl implements MerChantService {

    @Autowired
    private MerChantDao merChantDao;
    @Autowired
    private UserDao userDao;

    @Override
    public Result searchMerChat() {
        try {
            List<MerChant> allMerChants = merChantDao.findAllMerChants();
            return Result.ok(ResultCodeEnum.SUCCESS).data("allMerChants",allMerChants);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找出错");
        }
    }

    @Override
    public Result searchMerChatById(int mer_id) {
        try {
            MerChant merChant = merChantDao.findOne(mer_id);
            if (merChant!=null){
                return Result.ok().data("merChant",merChant);
            }else {
                return Result.error().message("未查询到该商铺信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找出错");
        }
    }

    @Override
    public Result update(MerChant merChant) {
        try{
            if (merChantDao.updateMer(merChant)>0){
                return Result.ok().message("修改成功");
            }
            else return Result.error().message("修改失败：未找到对应商铺");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应商铺");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result insert(MerChant merChant) {
        try{
            if (merChantDao.insert(merChant)>0){
                return Result.ok().message("添加成功");
            }
            else return Result.error().message("添加失败：商铺信息错误");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("添加失败：已经存在对应商铺");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("添加失败："+e.toString());
        }
    }

    @Override
    public Result delete(int mer_id) {
        try{
            if (merChantDao.delete(mer_id)>0){
                return Result.ok().message("删除成功");
            }
            else return Result.error().message("删除失败：该商铺不能删除，有设备还处于该商铺中");
        } catch (Exception e){
            System.err.println(e);
            return Result.error().message("删除失败："+e.toString());
        }
    }

    @Override
    public Result findAllMerChantsByKey(String key) {
        try {
            List<MerChant> allMerChants = merChantDao.findAllMerChantsByKey(key);
            return Result.ok(ResultCodeEnum.SUCCESS).data("allMerChants",allMerChants);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找出错");
        }
    }


}
